package com.sx.micowell.framework.tools;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.sx.micowell.R;

/**
 * @Author: YY
 * @Time: 2021/8/19
 * @Describe TODO
 */
public class AppDialogUtils {

    /**
     * 基础对话框
     * @param context
     */
    public static void showBasicDialog(Context context, String title, String content, IOnClickListener listener) {
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context, R.style.customTransparentDialog);

        View dialogView = ((Activity) context).getLayoutInflater().inflate(R.layout.dialog_basic_view, null);
        // 标题
        TextView titleView = dialogView.findViewById(R.id.txt_title);
        titleView.setText(title);
        // 内容
        TextView contentView = dialogView.findViewById(R.id.txt_message);
        contentView.setText(content);
        // 左按钮
        TextView leftView = dialogView.findViewById(R.id.txt_cancel);
        // 右按钮
        TextView rightView = dialogView.findViewById(R.id.txt_confirm);

        dialogBuilder.setView(dialogView);
        AlertDialog alertDialog = dialogBuilder.create();
        alertDialog.getWindow().setGravity(Gravity.CENTER);
        alertDialog.show();

        leftView.setOnClickListener(v -> {
            listener.leftListener();
            alertDialog.dismiss();
        });

        rightView.setOnClickListener(v -> {
            listener.rightListener();
            alertDialog.dismiss();
        });

        //设置大小
        WindowManager.LayoutParams layoutParams = alertDialog.getWindow().getAttributes();
        layoutParams.width = 4 * DisplayUtils.getScreenWidth(context) / 4;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        alertDialog.getWindow().setAttributes(layoutParams);
    }

    public static void showTipDialog(final Context context, String title, String message,  IOnClickListener listener) {
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context, R.style.customTransparentDialog);

        View dialogView = ((Activity) context).getLayoutInflater().inflate(R.layout.dialog_basic_view, null);
        // 标题
        TextView titleView = dialogView.findViewById(R.id.txt_title);
        titleView.setText(title);
        // 内容
        TextView contentView = dialogView.findViewById(R.id.txt_message);
        contentView.setText(message);

        // 左按钮
        TextView leftView = dialogView.findViewById(R.id.txt_cancel);
        leftView.setVisibility(View.GONE);

        // 右按钮
        TextView rightView = dialogView.findViewById(R.id.txt_confirm);
        dialogBuilder.setView(dialogView);
        AlertDialog alertDialog = dialogBuilder.create();
        alertDialog.setCancelable(false);
        alertDialog.getWindow().setGravity(Gravity.CENTER);
        alertDialog.show();

        rightView.setOnClickListener(v -> {
            listener.rightListener();
            alertDialog.dismiss();
        });

        //设置大小
        WindowManager.LayoutParams layoutParams = alertDialog.getWindow().getAttributes();
        layoutParams.width = 4 * DisplayUtils.getScreenWidth(context) / 4;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        alertDialog.getWindow().setAttributes(layoutParams);
    }


    public static void showPrivacyDialog(final Context context, IOnClickListener listener, ITextOnClickListener iTextOnClickListener) {
        textOnClickListener = iTextOnClickListener;

        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context, R.style.customTransparentDialog);

        View dialogView = ((Activity) context).getLayoutInflater().inflate(R.layout.dialog_privacy_view, null);
        // 标题
        TextView titleView = dialogView.findViewById(R.id.txt_title);
        // 内容
        TextView contentView = dialogView.findViewById(R.id.txt_message);
        setTextClick(context,contentView, contentView.getText().toString());

        // 左按钮
        TextView leftView = dialogView.findViewById(R.id.txt_cancel);
        // 右按钮
        TextView rightView = dialogView.findViewById(R.id.txt_confirm);

        dialogBuilder.setView(dialogView);
        AlertDialog alertDialog = dialogBuilder.create();
        alertDialog.getWindow().setGravity(Gravity.CENTER);
        alertDialog.show();

        leftView.setOnClickListener(v -> {
            listener.leftListener();
            alertDialog.dismiss();
        });

        rightView.setOnClickListener(v -> {
            listener.rightListener();
            alertDialog.dismiss();
        });

        //设置大小
        WindowManager.LayoutParams layoutParams = alertDialog.getWindow().getAttributes();
        layoutParams.width = 4 * DisplayUtils.getScreenWidth(context) / 4;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        alertDialog.getWindow().setAttributes(layoutParams);
    }

    /**
     * 更新对话框
     * @param context
     */
    public static void showUpdateDialog(Context context, String title, String content, IOnClickListener listener) {
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context, R.style.customTransparentDialog);

        View dialogView = ((Activity) context).getLayoutInflater().inflate(R.layout.dialog_update_view, null);
        // 标题
        TextView titleView = dialogView.findViewById(R.id.textView4);
        titleView.setText(title);
        // 内容
        TextView contentView = dialogView.findViewById(R.id.txt_message);
        contentView.setText(content);
        // 左按钮
        TextView leftView = dialogView.findViewById(R.id.txt_cancel);
        // 右按钮
        TextView rightView = dialogView.findViewById(R.id.txt_confirm);

        dialogBuilder.setView(dialogView);
        AlertDialog alertDialog = dialogBuilder.create();
        alertDialog.getWindow().setGravity(Gravity.CENTER);
        alertDialog.show();

        leftView.setOnClickListener(v -> {
            listener.leftListener();
            alertDialog.dismiss();
        });

        rightView.setOnClickListener(v -> {
            listener.rightListener();
            alertDialog.dismiss();
        });

        //设置大小
        WindowManager.LayoutParams layoutParams = alertDialog.getWindow().getAttributes();
        layoutParams.width = 4 * DisplayUtils.getScreenWidth(context) / 4;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        alertDialog.getWindow().setAttributes(layoutParams);
    }

    private static void setTextClick(Context context, TextView txtContent, String str) {
        SpannableStringBuilder ssb = new SpannableStringBuilder();
        ssb.append(str);
        //第一个出现的位置
        final int start = str.indexOf("《");
        ssb.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Log.e("onClick", "用户服务协议");
                if (textOnClickListener != null) {
                    textOnClickListener.onPrivacyOneClick();
                }

            }

            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(Color.parseColor("#FF3C51"));
                // 去掉下划线
                ds.setUnderlineText(false);
            }

        }, start, start + 8, 0);
        //最后一个出现的位置
        final int end = str.lastIndexOf("《");
        ssb.setSpan(new ClickableSpan() {

            @Override
            public void onClick(View widget) {
                Log.e("onClick", "隐私政策");
                if (textOnClickListener != null) {
                    textOnClickListener.onPrivacyTwoClick();
                }

            }
            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(Color.parseColor("#FF3C51"));
                // 去掉下划线
                ds.setUnderlineText(false);
            }

        }, end, end + 6, 0);
        //加上这句话才有效果
        txtContent.setMovementMethod(LinkMovementMethod.getInstance());
        //加上这句话才有效果
        txtContent.setText(ssb, TextView.BufferType.SPANNABLE);
        //去掉点击后的背景颜色为透明
        txtContent.setHighlightColor(ContextCompat.getColor(context, R.color.white));
    }

    public interface IOnClickListener {
        default void leftListener(){
        }
        void rightListener();
    }


    public interface ITextOnClickListener {
        void onPrivacyOneClick();
        void onPrivacyTwoClick();
    }

    private static ITextOnClickListener textOnClickListener;
}
